package com.jh3502.helper.function.person;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jh3502.helper.R;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author harisucici
 * @date 2017/5/22
 */

public class GridImageShowAdapter extends RecyclerView.Adapter<GridImageShowAdapter.ViewHolder> implements View.OnClickListener {
    Context mContext;

    private ArrayList<Map<String, Object>> listItems;
    private View convertView;
    @Nullable
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public GridImageShowAdapter(Context context, ArrayList<Map<String, Object>> datas) {
        this.mContext = context;
        this.listItems = datas;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_grid_image, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position + "");
        Glide.with(mContext).load(listItems.get(position).get("path")).into(viewHolder.itemOnlyImg);
    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((String) v.getTag());
        }
    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return listItems.size();
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(String s);
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_only_img)
        ImageView itemOnlyImg;

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}
